package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.api.Constans;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.Car;
import com.wqtz.chechai.bean.CarInfo;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.JsonUtils;
import com.wqtz.chechai.utils.NetworkUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.widget.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2017/2/20 17:21
 */

public class NewAddActivity extends BaseActivity implements NetworkUtils.GetJson {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.iv_vin)
	ImageView mIvVin;
	@BindView(R.id.iv_pinggu)
	ImageView mIvPinggu;
	@BindView(R.id.iv_driving_info)
	ImageView mIvDrivingInfo;
	@BindView(R.id.tv_vin)
	TextView mTvVin;
	@BindView(R.id.tv_edpg)
	TextView mTvEdpg;
	@BindView(R.id.tv_carinfo)
	TextView mTvCarinfo;
	@BindView(R.id.tv_vinnotice)
	TextView mTvVinnotice;
	@BindView(R.id.tv_vinsearchnotice)
	TextView mTvVinsearchnotice;
	@BindView(R.id.et_vin)
	ClearEditText mEtVin;
	@BindView(R.id.btn_search)
	Button mBtnSearch;
	@BindView(R.id.tv_searchResult)
	TextView mTvResult;
	@BindView(R.id.btn_confirm)
	Button mBtnConfirm;
	@BindView(R.id.tv_vin_error)
	TextView mTvVinError;
	@BindView(R.id.tv_vin_error_return)
	TextView mTvVinErrorReturn;
	//车辆信息
	CarInfo carInfo;
	Car mCar;
	@BindView(R.id.rv_title)
	RelativeLayout mRvTitle;
	@BindView(R.id.before_search)
	LinearLayout mBeforeSearch;
	@BindView(R.id.ll_aresult)
	LinearLayout mLlAresult;
	@BindView(R.id.after_search)
	LinearLayout mAfterSearch;
	private String url;
	private String vin;
	private String carModelId;
	@Override
	public void init() {
		ButterKnife.bind(this);
		mEtVin.addTextChangedListener(watcher);

	}

	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.fragment_newadd;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			//Logger.e(s.toString()+start+"  "+before+"  "+count);

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
		                              int after) {
			// TODO Auto-generated method stub
			//Logger.e(s.toString()+start+"  "+count);

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			Logger.e(s.toString());
			if (s.toString().toCharArray().length == 17) {
				vin = s.toString();
				mBtnSearch.setClickable(true);
				mBtnSearch.setBackgroundResource(R.drawable.shape_login_btn_pass);
			} else {
				mBtnSearch.setClickable(false);
				mBtnSearch.setBackgroundResource(R.drawable.shape_login_btn_default);
			}
			if (s.toString().toCharArray().length > 17) {
				ToastUtils.show(NewAddActivity.this, R.string.disablevin, 2000);
			}

		}
	};


	@OnClick({R.id.iv_back, R.id.btn_search, R.id.btn_confirm, R.id.tv_vin_error, R.id.tv_vin_error_return,R.id.tv_searchResult})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.btn_search:
				mBtnConfirm.setVisibility(View.GONE);
				//测试写死的VIN
				url = String.format(Constans.VIN_TO_CAR_INFO, "LSGGH59L9DS157185", Constans.VIN_TO_CAR_KEY);
				NetworkUtils.doHttpAsync(url, this, NewAddActivity.this);
				break;
			case R.id.tv_searchResult:{
				mBtnConfirm.setVisibility(View.VISIBLE);
			}break;
			case R.id.btn_confirm:
			case R.id.tv_vin_error:
				Intent intent = new Intent(this, NewAddStepTwoActivity.class);
				startActivity(intent);
				break;
			case R.id.tv_vin_error_return:
				mBeforeSearch.setVisibility(View.VISIBLE);
				mAfterSearch.setVisibility(View.GONE);
				break;

		}
	}

	@Override
	public void getJsonCallBack(String result, String request_url) {
		if (request_url.equals(url)) {
			carInfo = JsonUtils.getCarInfoBean(result);
			Logger.e(carInfo.toString());
			mTvResult.setText(carInfo.getResult().getBody().getCARINFO());
			mBeforeSearch.setVisibility(View.GONE);
			mAfterSearch.setVisibility(View.VISIBLE);
		} else {
			mTvResult.setText("未找到相关车型");
			mTvResult.setGravity(Gravity.CENTER);
		}

	}


}
